#include "custombutton.h"
#include <QPainter>
#include <QDebug>

//三态图片按钮
state3tbutton::state3tbutton(QString pixnormal,QWidget *parent):QPushButton(parent)
{
    this->setCursor(Qt::PointingHandCursor);
    m_index=0;
    m_enter=false;
    m_select = false;
    QPixmap pix(pixnormal);
    for(int i=0;i<3;i++)
        m_pixlist<<pix.copy(i*(pix.width()/3),0,pix.width()/3,pix.height());
}
void state3tbutton::setPixmap(QString pixnormal)
{
    if(!pixnormal.isEmpty())
    {
        QPixmap pix(pixnormal);
        m_pixlist.clear();
        for(int i=0;i<3;i++)
        m_pixlist<<pix.copy(i*(pix.width()/3),0,pix.width()/3,pix.height());
        update();
    }
}
void state3tbutton::isSelect(bool b_select)
{
    m_select = b_select;
    if(m_select)
    {
       m_index=2;
    }
    else
    {
       m_index=0;
    }
    update();
}
void state3tbutton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap((width()-m_pixlist.at(m_index).width())/2,(height()-m_pixlist.at(m_index).height())/2
                             ,m_pixlist.at(m_index).width()
                             ,m_pixlist.at(m_index).height(),m_pixlist.at(m_index));//画图画到中间
}
void state3tbutton::enterEvent(QEvent *)
{
    m_index=1;
    m_enter=true;
    update();
}
void state3tbutton::leaveEvent(QEvent *)
{
    if(m_select)
    {
       m_index=2;
    }
    else
    {
       m_index=0;
    }
    m_enter=false;
    update();
}
void state3tbutton::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton)//如果是左键按下
    {
        m_index=2;
        update();
        QPushButton::mousePressEvent(e);//返回到上级
    }
}
void state3tbutton::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton)//如果是左键放下
    {
        m_index=1;
        update();
       QPushButton::mouseReleaseEvent(e);//返回到上级 引出按钮clicked信号
    }
}
